package yannan.voiplock4ble.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;
import cn.sharesdk.onekeyshare.OnekeyShare;
import yannan.voiplock4ble.LoginActivity;
import yannan.voiplock4ble.MainActivity;
import yannan.voiplock4ble.R;
import yannan.voiplock4ble.UserCenterActivity;
import yannan.voiplock4ble.base.BaseFragment;
import yannan.voiplock4ble.base.Content;
import yannan.voiplock4ble.base.MyApplication;
import yannan.voiplock4ble.db.DaoUtils;
import yannan.voiplock4ble.db.daomanager.UserAccountDaoManager;
import yannan.voiplock4ble.net.NetUtils;
import yannan.voiplock4ble.utils.PopShowUtils;
import yannan.voiplock4ble.utils.ToastUtils;

/**
 * Created by PC on 2016/11/23.
 */
@Content(R.layout.fragment_menu_left)
public class LeftMenuFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.ll_head)
    LinearLayout ll_head;
    @Bind(R.id.tv_lock_record)
    TextView tvLockRecord;
    @Bind(R.id.tv_vien_key)
    TextView tvVienKey;
    @Bind(R.id.tv_pwd_key)
    TextView tvPwdKey;
    @Bind(R.id.tv_lock_list)
    TextView tvLockList;
//    @Bind(R.id.tv_about_us)
//    TextView tvAboutUs;
    @Bind(R.id.tv_out_line)
    TextView tvOutLine;
//    @Bind(R.id.share1)
//    ImageView share1;
    private MainActivity activity;
    int nowChecked;
    public int id;

    @Override
    public void initData() {
        onLeftMenuChecked = (MainActivity) getActivity();
        activity = (MainActivity) getActivity();
        ll_head.setOnClickListener(this);
        tvLockRecord.setOnClickListener(this);
        tvVienKey.setOnClickListener(this);
        tvPwdKey.setOnClickListener(this);
        tvLockList.setOnClickListener(this);
//        tvAboutUs.setOnClickListener(this);
        tvOutLine.setOnClickListener(this);
//        share1.setOnClickListener(this);
        setCheck(id);
        if (MyApplication.getInterface().userBean != null) {
            if (TextUtils.isEmpty(MyApplication.getInterface().userBean.nickName)) {
                tvPhone.setText(MyApplication.getInterface().userBean.userAccount);
            } else {
                tvPhone.setText(MyApplication.getInterface().userBean.userAccount + "\n" + MyApplication.getInterface().userBean.nickName);
            }
            if (!TextUtils.isEmpty(MyApplication.getInterface().userBean.headImageUrl)) {
                NetUtils.loadCircleImage(mContext, imgHead, R.drawable.ic_admin_head, NetUtils.Base_Url_Down + MyApplication.getInterface().userBean.headImageUrl);
            }
        }
    }

    @Override
    public void initView() {

    }

    public void setCheck(int id) {
        switch (id) {
            case R.id.ll_head:
                onClick(imgHead);
                break;
            case R.id.tv_lock_record:
                onClick(tvLockRecord);
                break;
            case R.id.tv_vien_key:
                onClick(tvVienKey);
                break;
            case R.id.tv_pwd_key:
                onClick(tvPwdKey);
                break;
            case R.id.tv_lock_list:
                onClick(tvLockList);
                break;
//            case R.id.tv_about_us:
//                onClick(tvAboutUs);
//                break;
            case R.id.tv_out_line:
                onClick(tvOutLine);
                break;
            default:
                onClick(tvLockList);
        }
    }

    @OnClick({R.id.left_menu_main})
    public void onClick(View v) {
        if (v.getId() == R.id.left_menu_main) {
            activity.closeDrawer();
        } else {
            switch (v.getId()) {
                case R.id.ll_head:
                    activity.startActivity(new Intent(activity, UserCenterActivity.class));
                    activity.closeDrawer();
                    break;
                case R.id.tv_lock_record:
                    if (checkIsHaveLock()) return;
                    if (nowChecked == v.getId()) {
                        activity.closeDrawer();
                        return;
                    }
                    setSelect(v.getId(), nowChecked);
                    nowChecked = v.getId();
                    onLeftMenuChecked.lockRecord();
                    break;
                case R.id.tv_vien_key:
                    if (checkIsHaveLock()) return;
                    if (nowChecked == v.getId()) {
                        activity.closeDrawer();
                        return;
                    }
                    setSelect(v.getId(), nowChecked);
                    nowChecked = v.getId();
                    onLeftMenuChecked.veinKey();
                    break;
                case R.id.tv_pwd_key:
                    if (checkIsHaveLock()) return;
                    if (nowChecked == v.getId()) {
                        activity.closeDrawer();
                        return;
                    }
                    setSelect(v.getId(), nowChecked);
                    nowChecked = v.getId();
                    onLeftMenuChecked.pwdKey();
                    break;
                case R.id.tv_lock_list:
                    if (nowChecked == v.getId()) {
                        activity.closeDrawer();
                        return;
                    }
                    setSelect(v.getId(), nowChecked);
                    nowChecked = v.getId();
                    onLeftMenuChecked.lockList();
                    break;
//                case R.id.tv_about_us:
//                    if (nowChecked == v.getId()) {
//                        activity.closeDrawer();
//                        return;
//                    }
//                    setSelect(v.getId(), nowChecked);
//                    nowChecked = v.getId();
//                    onLeftMenuChecked.aboutUs();
//                    break;
                case R.id.tv_out_line:
                    //// TODO: 2016/8/25 做一些退出登录的操作
                    PopShowUtils.showCommonDialog(mContext, "确定要退出么？", "取消", "确定", true, new PopShowUtils.IDialogCommonListener() {
                        @Override
                        public void onClick(int dir) {
                            // TODO: 2016/10/18 退出登录 清除登陆信息
                            if (dir == PopShowUtils.COMMON_DIALOG_RIGHT) {
                                DaoUtils.getDao(UserAccountDaoManager.class).deleteAll();
                                Intent intent = new Intent(mContext, LoginActivity.class);
                                startActivity(intent);
                                MainActivity activity = (MainActivity) getActivity();
                                activity.finish();
                            }
                        }
                    });
                    break;
//                case R.id.share1:
//                    showShare();
//                    break;

            }
        }
    }

    /**
     * 检查是否有门锁
     *
     * @return
     */
    private boolean checkIsHaveLock() {
        if (activity.lockBeanNow == null) {
            if (activity.lockBeanList == null || activity.lockBeanList.size() == 0) {
               ToastUtils.showToast(mContext, "请选择或添加门锁");
                
                return true;
            } else {
                activity.lockBeanNow = activity.lockBeanList.get(0);
            }
        }
        return false;
    }

    onLeftMenuChecked onLeftMenuChecked;

    public interface onLeftMenuChecked {
        /**
         * 门锁记录
         */
        void lockRecord();

        /**
         * 指静脉钥匙
         */
        void veinKey();

        /**
         * 密码钥匙
         */
        void pwdKey();

        /**
         * 添加设备
         */
        void lockList();

//        /**
//         * 关于我们
//         */
//        void aboutUs();

//        /**
//         * 个人中心
//         */
//        void userCenter();
    }

    private void setSelect(int checkID, int unCheckID) {
        try {
            TextView tvCheck = (TextView) findViewById(checkID);
            tvCheck.setTextColor(Color.GRAY);
//            tvCheck.setBackgroundColor(Color.WHITE);
        } catch (Exception e) {

        }
        try {
            if (unCheckID != 0) {
                TextView unCheck = (TextView) findViewById(unCheckID);
                unCheck.setTextColor(Color.WHITE);
                unCheck.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        } catch (Exception e) {

        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Object str) {
        if (str instanceof String) {
            if (str.equals("changeNickName")) {
                if (MyApplication.getInterface().userBean != null) {
                    tvPhone.setText(MyApplication.getInterface().userBean.userAccount + "\n" + MyApplication.getInterface().userBean.nickName);
                }
                if (MyApplication.getInterface().userBean.headImageUrl != null) {
                    NetUtils.loadCircleImage(mContext, imgHead, R.drawable.ic_admin_head, NetUtils.Base_Url_Down + MyApplication.getInterface().userBean.headImageUrl);
                }
            }
        }
    }


    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("智能门锁");
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://www.yannantech.com");
// text是分享文本，所有平台都需要这个字段
        oks.setText("智能门锁，智能门锁。牛叉牛叉");
        oks.setImageUrl("http://img.blog.csdn.net/20151224143520557");
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://www.yannantech.com");
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("智能门锁智能门锁智能门锁");
// site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("智能门锁");
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://www.yannantech.com");

// 启动分享GUI
        oks.show(mContext);
    }

}
